// pages/share/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    portrait: [
      { img: '../image/list_15_03.png', type: true }, { img: '', type: false }, { img: '', type: false },
      { img: '', type: false }, { img: '', type: false }, { img: '', type: false }, { img: '', type: false }
    ],
    second: '',
    set:'',
    attr_index:''
  },
  rule: function () {
    app.navTo('../rule/index')
  },
  //
  onurl:function(e){
    let that = this;
    console.log(that.data.ontype,'that.data.ontype')
    wx.navigateTo({
      url: '../confirmd/confirm?goodsid=' + that.data.content.goods_id + '&type=2&num=1&sid=' + that.data.content.sid + '&id=' + that.data.content.id + '&rule=' + that.data.attr_index + '&ontype=0'
    })
    // <navigator url="../confirmd/confirm?goodsid={{content.goodsid}}&type=2&rule={{_number2}}&num={{num}}&id={{item.id}}&sid={{sid}}" >
    // if(that.data.ontype == 0){
      
    // }else{
    //   wx.navigateTo({
    //     url: '../collageWare/collageWare?goodsid=' + this.data.content.goods_id + '&id=' + this.data.content.sid + '&ontype=0',
    //   })
    // }
    
  },


  //转时间
  uctime: function () {
    let that = this;
    var data = that.data.data;      //商品数据
    var time = that.data.time;      //时间
    var t;
    //console.log('执行')
    if (time == 0) {
      clearInterval(that.data.set)
      wx.request({
        url: app.data.Url + '/spell/SpellTimeOut',
        data: {
          suijizhi: data.suijizhi,
          sid: data.sid
        },
        method: 'post',
        success: function (res) {
          var data = '';
        }
      })
      wx.navigateTo({
        url: '../collageWare/collageWare?goodsid=' + that.data.content.goods_id + '&id=' + that.data.content.sid + '&ontype=0',
      })
    } else {
      time = time - 1
      var day = Math.floor(time / 3600 / 24);     //天
      //console.log(day,'------------')
      var hour = Math.floor(time / 3600) % 24;       //时
      //console.log(hour,'hourhour')
      var min = Math.floor(time / 60) % 60;     //分
      var sec = time % 60;                      //秒
      t = day + '天'
      if (hour < 10) {
        t += '0' + hour + "时";
      } else {
        t += hour + "时";
      }
      if (min < 10) { t += "0"; }
      t += min + "分";
      //console.log(t, 'ttttttttt')
      if (sec < 10) { t += "0"; }
      t += sec      //.toFixed(2);
      
      //console.log(t, '----t')
      that.setData({
        time: time,
        t:t
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // console.log(["1", "2", "3"].map(parseInt))
    // return false;
    // wx.showToast({
    //   title: options.id,
    // })
    wx.request({
      //url: app.data.Url + 'coupon/usercoupon',
      url: app.data.Url + 'spell/SpellGroupGhare',
      data: {
        orderid: options.id
      },
      method: 'get',
      success: function (res) {
        that.setData({
          data:res.data.data  
        })
        console.log(res.data.data.status,'res.data.data.status')
        if (res.data.data.status != 0){
          console.log(res.data.data.status)
          wx.showToast({
            title: '该商品拼团结束',
            icon: 'none',
            duration: 1000
          })
          var Numberofpeople = res.data.data.count - res.data.data.headlength;
          wx.request({
            url: app.data.Url + '/spell/SpellTimeOut',
            data: {
              suijizhi: res.data.data.suijizhi,
              sid: res.data.data.sid
            },
            method: 'post',
            success: function (res) {
              var data = '';
              //console.log(res.data.data)
            }
          })
          //console.log(Numberofpeople,'t人数')
          that.setData({
            content: res.data.data,
            Numberofpeople: Numberofpeople,
            time: '0',
            ontype:0
          })
          setTimeout(function(){
            wx.navigateTo({
              url: '../collageWare/collageWare?goodsid=' + that.data.content.goods_id + '&id=' + that.data.content.sid+'&ontype=0',
            })
          },1000)
        }else{
          var Numberofpeople = res.data.data.count - res.data.data.headlength;
          for (var i = 0; i < res.data.data.attr.length;i++){
            if (res.data.data.attrid == res.data.data.attr[i].attrid){
              console.log(i,'/*/*/-')
              that.setData({
                attr_index:i
              })
            }
          }
          //console.log(Numberofpeople,'t人数')
          that.setData({
            content: res.data.data,
            Numberofpeople: Numberofpeople,
            time: res.data.data.spellouttime,
          })
          //首次执行后定时器执行
          that.uctime()
          that.setData({
            set: setInterval(function () { that.uctime()},1000)
          })
        }
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  onHide:function(){
    console.log('页面隐藏')
    clearInterval(this.data.set)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('页面卸载')
    clearInterval(this.data.set)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    if (e.from === 'button') {

      // 来自页面内转发按钮
      console.log(e.target)
    }
    return {
      title: '广电智慧Life小程序',
      path: 'pages/share/index?id=' + e.target.dataset.id,
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
        })
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '转发失败',
        })
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  },
})
// uctime: function () {
//   let that = this;
//   var time = that.data.time;
//   var set;
//   var t;
//   time = time - 1
//   if (time > -1) {
//     var day = Math.floor(time / 3600 / 24);     //天
//     //console.log(day,'------------')
//     var hour = Math.floor(time / 3600) % 24;       //时
//     //console.log(hour,'hourhour')
//     var min = Math.floor(time / 60) % 60;     //分
//     var sec = time % 60;                      //秒
//     t = day + '天'
//     if (hour < 10) {
//       t += '0' + hour + "时";
//     } else {
//       t += hour + "时";
//     }
//     if (min < 10) { t += "0"; }
//     t += min + "分";
//     //console.log(t, 'ttttttttt')
//     if (sec < 10) { t += "0"; }
//     t += sec      //.toFixed(2);
//   }
//   //console.log(t, 't')
//   that.setData({
//     time: t
//   })
//   set = setInterval(function () {
//     console.log('执行')
//     if (time == 0) {
//       clearInterval(set)
//       console.log('结束')
//     } else {
//       let t;
//       time = time - 1
//       if (time > -1) {
//         var day = Math.floor(time / 3600 / 24);     //天
//         //console.log(day,'------------')
//         var hour = Math.floor(time / 3600) % 24;       //时
//         //console.log(hour,'hourhour')
//         var min = Math.floor(time / 60) % 60;     //分
//         var sec = time % 60;                      //秒
//         t = day + '天'
//         if (hour < 10) {
//           t += '0' + hour + "时";
//         } else {
//           t += hour + "时";
//         }
//         if (min < 10) { t += "0"; }
//         t += min + "分";
//         //console.log(t, 'ttttttttt')
//         if (sec < 10) { t += "0"; }
//         t += sec      //.toFixed(2);
//       }
//       //console.log(t, 't')
//       that.setData({
//         time: t
//       })
//     }
//   }, 1000)
// },